package com.lhoverc.aop;

import com.lhoverc.pojo.entity.AnnouncementEntity;
import com.lhoverc.pojo.entity.ApplyEntity;
import com.lhoverc.pojo.entity.UserEntity;
import com.lhoverc.pojo.entity.VisitorEntity;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;

/**
 * @Author: LiCheng
 * @CreateTime: 2025-04-19
 * @Description: 添加日期的aop切面类，在新增操作前设置创建时间和修改时间
 * @Version: 1.0
 */
@Slf4j
@Aspect
@Component
public class DateTimeAspect {
    @Pointcut("@annotation(com.lhoverc.annotation.AddDateTime)")
    private void addDateTimePointCut() {}

    @Pointcut("@annotation(com.lhoverc.annotation.AddUpdateTime)")
    private void addUpdateTimePointCut() {}



    /**
     * 前置通知：在目标方法执行前修改参数
     */
    @Before("addDateTimePointCut()")
    public void addDateTime(JoinPoint joinPoint) {
        Object[] args = joinPoint.getArgs();
        for (Object arg : args) {
            if (arg instanceof UserEntity userEntity) { // 找到第一个UserEntity即可退出
                LocalDateTime now = LocalDateTime.now();
                userEntity.setCreateTime(now);
                userEntity.setUpdateTime(now);
                break;
            }else if (arg instanceof AnnouncementEntity announcementEntity) { // 找到第一个AnnouncementEntity即可退出
                LocalDateTime now = LocalDateTime.now();
                announcementEntity.setCreateTime(now);
                announcementEntity.setUpdateTime(now);
                break;
            }else if (arg instanceof ApplyEntity applyEntity) { // 找到第一个ApplyEntity即可退出
                LocalDateTime now = LocalDateTime.now();
                applyEntity.setCreateTime(now);
                applyEntity.setUpdateTime(now);
                break;
            }else if (arg instanceof VisitorEntity visitorEntity) { // 找到第一个VisitorEntity即可退出
                LocalDateTime now = LocalDateTime.now();
                visitorEntity.setCreateTime(now);
                visitorEntity.setUpdateTime(now);
                break;
            } else if (arg instanceof ApplyEntity applyEntity) { // 找到第一个VisitorEntity即可退出
                LocalDateTime now = LocalDateTime.now();
                applyEntity.setCreateTime(now);
                applyEntity.setUpdateTime(now);
                break;
            }
        }
    }


    /**
     * 前置通知：在目标方法执行前修改参数
     */
    @Before("addUpdateTimePointCut()")
    public void addUpdateTime(JoinPoint joinPoint) {
        Object[] args = joinPoint.getArgs();
        for (Object arg : args) {
            if (arg instanceof UserEntity userEntity) { // 找到第一个UserEntity即可退出
                LocalDateTime now = LocalDateTime.now();
                userEntity.setUpdateTime(now);
                break;
            }else if (arg instanceof AnnouncementEntity announcementEntity) { // 找到第一个AnnouncementEntity即可退出
                LocalDateTime now = LocalDateTime.now();
                announcementEntity.setUpdateTime(now);
                break;
            }else if (arg instanceof ApplyEntity applyEntity) { // 找到第一个ApplyEntity即可退出
                LocalDateTime now = LocalDateTime.now();
                applyEntity.setUpdateTime(now);
                break;
            }else if (arg instanceof VisitorEntity visitorEntity) { // 找到第一个VisitorEntity即可退出
                LocalDateTime now = LocalDateTime.now();
                visitorEntity.setUpdateTime(now);
                break;
            } else if (arg instanceof ApplyEntity applyEntity) { // 找到第一个VisitorEntity即可退出
                LocalDateTime now = LocalDateTime.now();
                applyEntity.setUpdateTime(now);
                break;
            }
        }
    }

}
